package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.CreateCustomerFileRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteCustomerFileRequest;
import com.haohan.jingyuan.customer.dto.req.QueryCustomerFileListByCustomerIdRequest;
import com.haohan.jingyuan.customer.dto.req.QueryCustomerFileListRequest;
import com.haohan.jingyuan.customer.dto.req.UpdateCustomerFileRequest;
import com.haohan.jingyuan.customer.dto.resp.CustomerFileListResp;
import com.haohan.jingyuan.customer.dto.resp.CustomerFileResp;
import com.haohan.jingyuan.customer.dto.resp.CustomerFileTreeResp;
import com.haohan.jingyuan.customer.service.CustomerFileService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户材料控制器
 *
 * @ClassName CustomerFileController
 * @Description 客户材料相关接口控制器
 * @Author hexijun
 * @Time 2025/01/31 10:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerFileController {

    /**
     * 客户材料服务
     */
    private final CustomerFileService customerFileService;

    /**
     * 上传文件
     * 
     * 注意：tag字段传入的是customer_tag表的tag_code列表，每个tag_code的type必须为2（客户材料标签）
     *
     * @param request 上传文件请求
     * @return 上传结果
     */
    @PostMapping("/customer/files/create")
    @NeedLogin
    public GwResponse<CustomerFileResp> uploadFile(@Valid @RequestBody GwRequest<CreateCustomerFileRequest> request) {
        log.info("CustomerFileController.uploadFile BEGIN ==> {}", JSONUtil.toJsonStr(request));

        CustomerFileResp customerFileResp = customerFileService.createCustomerFile(request.getRequestData());
        GwResponse<CustomerFileResp> response = GwResponse.success("上传文件成功", customerFileResp);

        log.info("CustomerFileController.uploadFile END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 编辑文件
     * 
     * 注意：tag字段传入的是customer_tag表的tag_code列表，每个tag_code的type必须为2（客户材料标签）
     *
     * @param request 编辑文件请求
     * @return 编辑结果
     */
    @PostMapping("/customer/files/update")
    @NeedLogin
    public GwResponse<CustomerFileResp> updateFile(@Valid @RequestBody GwRequest<UpdateCustomerFileRequest> request) {
        log.info("CustomerFileController.updateFile BEGIN ==> {}", JSONUtil.toJsonStr(request));

        CustomerFileResp customerFileResp = customerFileService.updateCustomerFile(request.getRequestData());
        GwResponse<CustomerFileResp> response = GwResponse.success("编辑文件成功", customerFileResp);

        log.info("CustomerFileController.updateFile END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除文件
     *
     * @param request 删除文件请求
     * @return 删除结果
     */
    @PostMapping("/customer/files/delete")
    @NeedLogin
    public GwResponse<String> deleteFile(@Valid @RequestBody GwRequest<DeleteCustomerFileRequest> request) {
        log.info("CustomerFileController.deleteFile BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerFileService.deleteCustomerFile(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除文件成功", null);
        } else {
            response = GwResponse.error("删除文件失败");
        }

        log.info("CustomerFileController.deleteFile END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 根据客户ID查询客户材料列表（树状结构）
     *
     * @param request 查询客户材料列表请求
     * @return 客户材料树状结构
     */
    @PostMapping("/customer/files/listByCustomerId")
    @NeedLogin
    public GwResponse<CustomerFileTreeResp> queryFileListByCustomerId(@Valid @RequestBody GwRequest<QueryCustomerFileListByCustomerIdRequest> request) {
        log.info("CustomerFileController.queryFileListByCustomerId BEGIN ==> {}", JSONUtil.toJsonStr(request));

        CustomerFileTreeResp fileTree = customerFileService.queryFileListByCustomerId(request.getRequestData());
        GwResponse<CustomerFileTreeResp> response = GwResponse.success("查询客户材料列表成功", fileTree);

        log.info("CustomerFileController.queryFileListByCustomerId END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询客户材料列表（分页，平铺展示）
     *
     * @param request 查询客户材料列表请求
     * @return 客户材料列表
     */
    @PostMapping("/customer/files/list")
    @NeedLogin
    public GwResponse<CustomerFileListResp> queryFileList(@Valid @RequestBody GwRequest<QueryCustomerFileListRequest> request) {
        log.info("CustomerFileController.queryFileList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        CustomerFileListResp fileList = customerFileService.queryFileList(request.getRequestData());
        GwResponse<CustomerFileListResp> response = GwResponse.success("查询客户材料列表成功", fileList);

        log.info("CustomerFileController.queryFileList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}

